﻿using Domain.Model.DTO;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Filters;

namespace Web.Controllers
{
    /// <summary>
    /// 网络图
    /// </summary>
    public class ChartController : LoginedController
    {
        [ActionLog("推荐网络图")]       
        public ActionResult Index()
        {
            var cusNum = this.DbContext.Database.NewCustormerNo();

            return View();  
        }

        private bool HasParents(List<RecommendView> data, RecommendView view, Guid pid)
        {
            if (view.RId == pid)
            {
                return true;
            }
            else if (view.RId == Guid.Empty)
            {
                return false;
            }

            var v = data.Find(item => item.MyId == view.RId);
            return HasParents(data, v, pid);
        }

        [HttpPost]
        public ActionResult GetNodes(Guid? pid)
        {
            var parentId = pid.HasValue ? pid.Value : Guid.Empty;

            var data = this.DbContext.Recommend.Select(item => new RecommendView
            {
                MyId = item.MyId,
                RId = item.RecommenderId,
                MyAccount = item.My.Account,
                MyNickName = item.My.NickName
            }).ToList();

            var model = data.Where(item => this.HasParents(data, item, parentId)).Select(item => new
            {
                id = item.MyId,
                pId = item.RId,
                name = item.MyAccount + "/" + item.MyNickName,
                icon = "/image/userblue.png"
            });
            return Json(model);

        }

        public ActionResult UserDetail(Guid id)
        {
            var model = this.DbContext.User.Find(id);

            return View(model);
        }
    }
}
